package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.BpmnInfo;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.IBpmnInfoService;
import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;



@RequestMapping("/business/serviceItem")
@Controller
public class ServiceItemController extends BaseController {
    String prefix = "business/serviceItem/";
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private ISysDictDataService sysDictDataService;
    @Autowired
    private ISysUserService userService;

    //页面响应
    @RequestMapping("/listPage")
    public String list(){
        return prefix + "list";
    }
    //数据列表查询
    @RequestMapping("/query")
    @ResponseBody
    public TableDataInfo query(ServiceItem serviceItem){
        startPage();
        List<ServiceItem> list = serviceItemService.selectServiceItemList(serviceItem);
        return getDataTable(list);
    }

    //养修单项新增页面
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }
    //养修单项编辑页面
    @RequestMapping("/editPage/{id}")
    public String editPage(@PathVariable("id") Long id, Model model){
        if (id == null){
            throw new ServiceException("参数传递错误");
        }
        ServiceItem serviceItem = serviceItemService.selectServiceItemById(id);
        model.addAttribute("serviceItem",serviceItem);
        return prefix + "edit";

    }
    //数据添加
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(ServiceItem serviceItem){
        serviceItemService.saverServiceItem(serviceItem);
        return AjaxResult.success();
    }
    //数据编辑
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ServiceItem serviceItem){
        serviceItemService.update(serviceItem);
        return AjaxResult.success();
    }
    //养修单项删除
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long ids){
        serviceItemService.updateByDelete(ids);
        return AjaxResult.success();
    }
    //养修单项上架
    @RequestMapping("/ItemUp")
    @ResponseBody
    public AjaxResult ItemUp(Long id){
        if (id == null){
            throw new ServiceException("操作失败");
        }
        serviceItemService.updateSaleStatus(id,ServiceItem.SALESTATUS_ON);
        return AjaxResult.success();
    }
    //养修单项下架
    @RequestMapping("/ItemNext")
    @ResponseBody
    public AjaxResult ItemNext(Long id){
        if (id == null){
            throw new ServiceException("操作失败");
        }
        serviceItemService.updateSaleStatus(id,ServiceItem.SALESTATUS_OFF);
        return AjaxResult.success();
    }

    @RequestMapping("/auditPage/{id}")
    public String auditPage(@PathVariable("id") Long id,Model model){
        ServiceItem serviceItem = serviceItemService.selectServiceItemById(id);
        model.addAttribute("serviceItem",serviceItem);
        BpmnInfo bpmnInfo = bpmnInfoService.getByBpmnType("car_package");
        bpmnInfo.setBpmnName(sysDictDataService.selectDictLabel("bus_audit_type","car_package"));
        model.addAttribute("bpmnInfo",bpmnInfo);
        //店长directors
        List<SysUser> directors = userService.queryByRoleKey("shopOwner");
        //财务finances
        List<SysUser> finances = userService.queryByRoleKey("financial");
        model.addAttribute("directors",directors);
        model.addAttribute("finances",finances);
        return prefix+"audit";
    }



}
